<template>
  <view class="container">
    <!-- 反馈类型 -->
    <view class="form-section">
      <view class="section-title">反馈类型</view>
      <view class="type-list">
        <view 
          class="type-item" 
          v-for="(item, index) in typeList" 
          :key="index"
          :class="{ active: form.type === item.value }"
          @tap="selectType(item.value)"
        >
          <text>{{item.label}}</text>
        </view>
      </view>
    </view>
    
    <!-- 反馈内容 -->
    <view class="form-section">
      <view class="section-title">反馈内容</view>
      <textarea 
        v-model="form.content"
        placeholder="请详细描述您遇到的问题或建议"
        maxlength="500"
        class="content-input"
      ></textarea>
      <view class="word-count">{{form.content.length}}/500</view>
    </view>
    
    <!-- 图片上传 -->
    <view class="form-section">
      <view class="section-title">上传图片</view>
      <view class="upload-list">
        <view 
          class="upload-item" 
          v-for="(item, index) in form.images" 
          :key="index"
        >
          <image :src="item" mode="aspectFill"></image>
          <text class="iconfont icon-delete" @tap="deleteImage(index)"></text>
        </view>
        <view 
          class="upload-btn" 
          v-if="form.images.length < 3"
          @tap="chooseImage"
        >
          <text class="iconfont icon-add"></text>
          <text>上传图片</text>
        </view>
      </view>
      <view class="upload-tip">最多上传3张图片</view>
    </view>
    
    <!-- 联系方式 -->
    <view class="form-section">
      <view class="section-title">联系方式</view>
      <input 
        type="text" 
        v-model="form.contact"
        placeholder="请留下您的手机号或邮箱，方便我们联系您"
        class="contact-input"
      />
    </view>
    
    <!-- 提交按钮 -->
    <view class="submit-btn" @tap="handleSubmit">
      <text>提交反馈</text>
    </view>
  </view>
</template>

<script>
import { userApi } from '@/api/index'
import { showToast } from '@/utils/common'

export default {
  data() {
    return {
      typeList: [
        { label: '功能异常', value: 'bug' },
        { label: '体验问题', value: 'experience' },
        { label: '产品建议', value: 'suggestion' },
        { label: '其他问题', value: 'other' }
      ],
      form: {
        type: '',
        content: '',
        images: [],
        contact: ''
      }
    }
  },
  methods: {
    // 选择反馈类型
    selectType(type) {
      this.form.type = type
    },
    
    // 选择图片
    chooseImage() {
      uni.chooseImage({
        count: 3 - this.form.images.length,
        success: (res) => {
          this.form.images = [...this.form.images, ...res.tempFilePaths]
        }
      })
    },
    
    // 删除图片
    deleteImage(index) {
      this.form.images.splice(index, 1)
    },
    
    // 表单验证
    validate() {
      if (!this.form.type) {
        showToast('请选择反馈类型')
        return false
      }
      if (!this.form.content) {
        showToast('请输入反馈内容')
        return false
      }
      if (this.form.content.length < 10) {
        showToast('反馈内容不能少于10个字')
        return false
      }
      return true
    },
    
    // 提交表单
    async handleSubmit() {
      if (!this.validate()) return
      
      try {
        await userApi.submitFeedback(this.form)
        showToast('反馈提交成功')
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
      } catch (error) {
        showToast('反馈提交失败')
      }
    }
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20rpx;
}

.form-section {
  background: #fff;
  border-radius: 12rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  
  .section-title {
    font-size: 28rpx;
    color: #333;
    font-weight: bold;
    margin-bottom: 20rpx;
  }
  
  .type-list {
    display: flex;
    flex-wrap: wrap;
    
    .type-item {
      width: 160rpx;
      height: 60rpx;
      line-height: 60rpx;
      text-align: center;
      background: #f5f5f5;
      border-radius: 30rpx;
      margin-right: 20rpx;
      margin-bottom: 20rpx;
      
      text {
        font-size: 24rpx;
        color: #666;
      }
      
      &.active {
        background: #ff0000;
        
        text {
          color: #fff;
        }
      }
    }
  }
  
  .content-input {
    width: 100%;
    height: 300rpx;
    background: #f5f5f5;
    border-radius: 12rpx;
    padding: 20rpx;
    font-size: 28rpx;
    color: #333;
  }
  
  .word-count {
    text-align: right;
    font-size: 24rpx;
    color: #999;
    margin-top: 10rpx;
  }
  
  .upload-list {
    display: flex;
    flex-wrap: wrap;
    
    .upload-item {
      position: relative;
      width: 200rpx;
      height: 200rpx;
      margin-right: 20rpx;
      margin-bottom: 20rpx;
      
      image {
        width: 100%;
        height: 100%;
        border-radius: 8rpx;
      }
      
      .icon-delete {
        position: absolute;
        right: -10rpx;
        top: -10rpx;
        width: 40rpx;
        height: 40rpx;
        line-height: 40rpx;
        text-align: center;
        background: rgba(0, 0, 0, 0.5);
        border-radius: 50%;
        color: #fff;
        font-size: 24rpx;
      }
    }
    
    .upload-btn {
      width: 200rpx;
      height: 200rpx;
      background: #f5f5f5;
      border-radius: 8rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      
      .icon-add {
        font-size: 48rpx;
        color: #999;
        margin-bottom: 10rpx;
      }
      
      text {
        font-size: 24rpx;
        color: #999;
      }
    }
  }
  
  .upload-tip {
    font-size: 24rpx;
    color: #999;
    margin-top: 10rpx;
  }
  
  .contact-input {
    width: 100%;
    height: 80rpx;
    background: #f5f5f5;
    border-radius: 12rpx;
    padding: 0 20rpx;
    font-size: 28rpx;
    color: #333;
  }
}

.submit-btn {
  margin-top: 40rpx;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  background: #ff0000;
  color: #fff;
  border-radius: 40rpx;
  font-size: 28rpx;
}
</style> 